home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / ImagePreview.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  170 lines

  1. /*
  2.  * @(#)ImagePreview.java    1.6 01/28/98
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  * @author James Gosling
  19.  * @author Ray Ryan
  20.  */
  21.  
  22. package com.sun.java.swing;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.awt.image.*;
  27. import java.io.File;
  28.  
  29. import com.sun.java.swing.event.*;
  30.  
  31. /** A component to preview an image file, intended to serve as
  32.  *  an accessory to a JFileChooser.
  33.  */
  34. public class JImagePreviewer extends JComponent implements ActionListener {
  35.     private Image img;
  36.     private boolean imgKnown;
  37.     private long lastUpdateTime = 0;
  38.     private static Dimension isize = new Dimension(120, 120);
  39.     private boolean blank;
  40.  
  41. //     private static FileChooserFilter fcf;
  42.  
  43. //     /** Convenience method to prompt for the name of an image file.
  44. //     @param fparent the parent frame for the dialog box.
  45. //         fparent may be null if a default parent has
  46. //         been established with StandardDialog
  47. //     @param description a description string that will be shown
  48. //         to the user to indicate what is being requested
  49. //     @param initial the initial value for the string
  50. //     @param columns the default width (in 'm's) of the TextField
  51. //     @param target the ChangeListener that will be informed if the
  52. //             OK or Apply buttons are hit.
  53. //     @return If target is null,
  54. //         the dialog box will be modal and the method
  55. //         will return the final result, or null if canceled.
  56. //         Otherwise, the
  57. //         dialog box is non-modal, the method returns null
  58. //         immediatly, and the listener is informed when
  59. //         appropriate.  The source of the change event will
  60. //         be a FileChooser.
  61. //     @see StandardDialog
  62. //      */
  63. //     public static File ask(Component parent, String title, File dfc,
  64. //                     ChangeListener target) {
  65. //     StandardDialog tDialog;
  66. //     FileChooser tChooser;
  67. //     tChooser = new FileChooser();
  68. //     tDialog = new StandardDialog(parent, tChooser, target == null);
  69. //     if (fcf == null) {
  70. //         fcf = new FileChooserFilter();
  71. //         fcf.add("Web images (gif,jpg,jpeg)");
  72. //     }
  73. //     tChooser.setFilter(fcf);
  74. //     tChooser.setPreview(new JImagePreviewer ());
  75. //     if (target != null)
  76. //         tDialog.addChangeListener(target);
  77. //     if (title != null)
  78. //         tDialog.setTitle(title);
  79. //     tChooser.getModel().setFile(dfc);
  80. //     tDialog.start();
  81. //     File r = tDialog.isCanceled() || target != null ? null
  82. //     : tChooser.getModel().getFile();
  83. //     if (target == null)
  84. //         tDialog.dispose();
  85. //     return r;
  86. //     }
  87.  
  88.     public JImagePreviewer () {
  89.     }
  90.  
  91.     public void setPreviewFile(File f) {
  92.     System.out.println("Let's preview file " + f);
  93.     if (f == null) {
  94.         if (!blank) {
  95.         blank = true;
  96.         repaint();
  97.         }
  98.         return;
  99.     }
  100.     blank = false;
  101.     img = getToolkit().getImage(f.getPath());
  102.     this.img = img;
  103.     if (isShowing())
  104.         repaint(10);
  105.     }
  106.  
  107.     public void actionPerformed(ActionEvent evt) {
  108.     Object source = evt.getSource();
  109.     if (source instanceof JDirectoryPane) {
  110.         this.setPreviewFile(((JDirectoryPane)source).getSelectedFile());
  111.     }
  112.     }
  113.  
  114.     public Dimension getPreferredSize() {
  115.     return isize;
  116.     }
  117.  
  118.     public Dimension getMinimumSize() {
  119.     return getPreferredSize();
  120.     }
  121.  
  122.     public synchronized boolean imageUpdate(Image img,
  123.                         int infoflags,
  124.                         int x, int y,
  125.                         int width, int height) {
  126.     if (img != this.img)
  127.         return false;
  128.     long t = System.currentTimeMillis();
  129.     if (lastUpdateTime + 500 < t || (infoflags & (ALLBITS | FRAMEBITS | ERROR | ABORT)) != 0) {
  130.         repaint(10);
  131.         lastUpdateTime = t;
  132.     }
  133.     if ((infoflags & (ERROR | ABORT)) != 0) {
  134.         img = null;
  135.         return false;
  136.     }
  137.     return (infoflags & ALLBITS) == 0;
  138.     }
  139.  
  140.     public void paint(Graphics g) {
  141.     if (blank) 
  142.         return;
  143.  
  144.     Image i = img;
  145.     Dimension d = getSize();
  146.     if (i == null)
  147.         return;
  148.     int w = i.getWidth(this);
  149.     int h = i.getHeight(this);
  150.     int iw, ih;
  151.     float aspect = (float) w / (float) h;
  152.     int dh = (int) (d.width / aspect);
  153.     int dw = (int) (d.height * aspect);
  154.     if (dh > d.height) {
  155.         iw = dw;
  156.         ih = d.height;
  157.     } else {
  158.         iw = d.width;
  159.         ih = dh;
  160.     }
  161.     if (iw >= w && ih >= h) {
  162.         iw = w;
  163.         ih = h;
  164.     }
  165.     if (w < 0 || h < 0)
  166.         return;
  167.     g.drawImage(i, (d.width - iw) >> 1, (d.height - ih) >> 1, iw, ih, this);
  168.     }
  169. }
  170.